import os
import os.path
from os import listdir
from os.path import isfile, join ,isdir
import time


def folderScanner(path):
    folder_list = []
    for f in listdir(path):
        if os.path.isdir(path + "\\" + f):
            folder_list.append(f)
    return folder_list


def fileScanner(path, folder_name):
    file_list = []
    for f in listdir(path + "\\" + folder_name):
        file_list.append(f)
    return file_list

def createGalleryCode(file_list, index, title, path):
    html_str = "<html>" + "\n"
    html_str = html_str + " " + "<head>" + "\n"
    html_str = html_str + "     " + "<title>" + "\n"
    html_str = html_str + "     " + title + index + "\n"
    html_str = html_str + "     " + "</title>" + "\n"
    html_str = html_str + " " + "</head>" + "\n"
    html_str = html_str + " " + "<body>" + "\n"
    for f in file_list:
        html_str = html_str + "    " + "<img src=\"./" + index + "/" + f + "\">" + "<br/>\n"
    html_str = html_str + " " + "</body>" + "\n"
    html_str = html_str + "</html>"
    # return html_str
    with open(path + "\\" + index + ".html", 'w') as file:
        print(path + "\\" + index + ".html")
        file.write(html_str)


if __name__ == '__main__':
    title = '老婆的姊姊'
    folder_name = r'D:/data/漫画/'
    folders = folderScanner(folder_name + title)
    index = 1
    for folder in folders:
        files = fileScanner(folder_name + title, str(index).zfill(3))
        createGalleryCode(files, str(index).zfill(3), title, folder_name + title)
        index = index + 1
    print("Done!")
